<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 

        12. 数组按照指定顺序排序，将原数组内的元素，分别从中间向两边交替追加，
        例如:[0,1,2,3,4]返回[4,2,0,1,3][0,1,2,3,4,5,6,7,8]返回[8,6,4,2.0,1,3,5,7];(来自腾讯)
     -->
    <script>
      function arrayFn(arr) {
        if (arr.length % 2 === 0) {
          console.error('不可是偶数');
          return;
        }
        let l = (arr.length - 1) / 2;
        let r = l;
        let index = 0;
        const newArr = Array.from({ length: arr.length });

        while (l >= 0 && r <= arr.length) {
          if (l === r) {
            newArr[l] = arr[index];
            l--;
            r++;
          } else {
            if (index % 2 === 0) {
              newArr[l] = arr[index];
              l--;
            } else {
              newArr[r] = arr[index];
              r++;
            }
          }
          index++;
        }
        return newArr;
      }

      console.log(arrayFn([0, 1, 2, 3, 4, 5, 6, 7, 8]));
    </script>
  </body>
</html>
